<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="项目编码" prop="grantCode">
        <el-input
          v-model="queryParams.grantCode"
          placeholder="请输入项目编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目名称" prop="grantName">
        <el-input
          v-model="queryParams.grantName"
          placeholder="请输入项目名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目类型" prop="granttypeId">
        <el-select v-model="queryParams.granttypeId" placeholder="请选择项目类型">
          <el-option
            v-for="dict in grantTypeOption"
            :key="dict.id"
            :label="dict.name"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="项目状态" prop="isEnabled">
        <el-select v-model="queryParams.isEnabled" placeholder="请选择考项目状态" clearable size="small">
          <el-option
            v-for="dict in disableOption"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否开启" prop="upStatus">
        <el-select v-model="queryParams.upStatus" placeholder="请选择考是否开启" clearable size="small">
          <el-option
            v-for="dict in grantInputTypeOption"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="照片上传" prop="isUpphoto">
        <el-select v-model="queryParams.isUpphoto" placeholder="请选择考照片上传" clearable size="small">
          <el-option
            v-for="dict in yesnoOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['grant:grant:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['grant:grant:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['grant:grant:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['grant:grant:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="grantList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="grantId" />
      <el-table-column label="项目编码" align="center" prop="grantCode" />
      <el-table-column label="项目名称" align="center" prop="grantName" />
      <el-table-column label="项目类型" align="center" prop="granttypeId" :formatter="granttypeFormat"/>
      <el-table-column label="创建人" align="center" prop="createUser" />
      <el-table-column label="是否可用" align="center" prop="isEnabled" :formatter="enableFormat"/>
      <el-table-column label="是否开启" align="center" prop="upStatus" />
      <el-table-column label="照片上传" align="center" prop="isUpphoto" :formatter="typeFormat"/>
      <el-table-column label="阅卷天数" align="center" prop="signDay" />
      <el-table-column label="组长每天费用" align="center" prop="prefectPrice" />
      <el-table-column label="优秀每天费用" align="center" prop="excellentPrice" />
      <el-table-column label="考官每天费用" align="center" prop="examinerPrice" />
      <el-table-column label="免税额度" align="center" prop="dutyFreePrice" />
      <el-table-column label="税率%" align="center" prop="taxRate" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['grant:grant:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['grant:grant:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改报名项目对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1024px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="项目编码" prop="grantCode">
          <el-input v-model="form.grantCode" placeholder="请输入考官上报状态" />
        </el-form-item>
        <el-form-item label="项目名称" prop="grantName">
          <el-input v-model="form.grantName" placeholder="请输入考官上报状态" />
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="项目类型" prop="granttypeId">
              <el-select v-model="form.granttypeId" placeholder="请选择项目类型">
                <el-option
                  v-for="dict in grantTypeOption"
                  :key="dict.id"
                  :label="dict.name"
                  :value="dict.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否可用" prop="isEnabled">
              <el-select v-model="form.isEnabled" placeholder="请选择是否启用">
                <el-option
                  v-for="dict in disableOption"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否开启">
              <el-select v-model="form.upStatus" placeholder="请选择是否启用">
                <el-option
                  v-for="dict in grantInputTypeOption"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="照片上传" prop="isUpphoto">
              <el-select v-model="form.isUpphoto" placeholder="请选择照片上传状态">
                <el-option
                  v-for="dict in yesnoOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="阅卷天数" prop="signDay">
              <el-input-number v-model="form.signDay" :precision="0" :step="1"  placeholder="请输入阅卷天数" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="组长每天费用" prop="prefectPrice">
              <el-input-number v-model="form.prefectPrice" :precision="2" :step="0.1" placeholder="请输入组长每天费用" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="优秀每天费用" prop="excellentPrice">
              <el-input-number v-model="form.excellentPrice" :precision="2" :step="0.1" placeholder="请输入优秀每天费用" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="考官每天费用" prop="examinerPrice">
              <el-input-number v-model="form.examinerPrice" :precision="2" :step="0.1" placeholder="请输入考官每天费用" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="免税额度" prop="dutyFreePrice">
              <el-input-number v-model="form.dutyFreePrice" :precision="2" :step="0.1" placeholder="请输入免税额度" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="税率%" prop="taxRate">
              <el-input-number v-model="form.taxRate" :precision="2" :step="0.1" placeholder="请输入税率" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="项目说明" prop="grantContent">
          <quill-editor id="quillEditor1" @getcode="getContent" :contentDefault="form.grantContent"></quill-editor>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listGrant, getGrant, delGrant, addGrant, updateGrant, exportGrant } from "@/api/grant/grant";
import {listGranttype} from "@/api/grant/granttype"
import quillEditor from "@/views/components/quillEditor";

export default {
  name: "Grant",
  components:{quillEditor},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 报名项目表格数据
      grantList: [],
      grantInputTypeOption:[],
      yesnoOptions:[],
      disableOption:[],
      grantTypeOption:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        grantCode: undefined,
        grantName: undefined,
        granttypeId: undefined,
        grantContent: undefined,
        departId: undefined,
        createUser: undefined,
        isEnabled: undefined,
        upStatus: undefined,
        isUpphoto: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        grantCode: [
          { required: true, message: "考官上报状态不能为空", trigger: "blur" }
        ],
        createUser: [
          { required: true, message: "考官上报状态不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getDicts("sys_yes_no").then(response => {
      this.yesnoOptions = response.data;
    });
    this.getDicts("grant_input_type").then(response => {
      this.grantInputTypeOption = response.data;
    });
    this.getDicts("sys_normal_disable").then(response => {
      this.disableOption = response.data;
    });
    listGranttype().then(response=>{
      this.grantTypeOption = response.rows;
    })
    this.getList();
  },
  methods: {
    /** 查询报名项目列表 */
    getList() {
      this.loading = true;
      listGrant(this.queryParams).then(response => {
        this.grantList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        grantId: undefined,
        grantCode: undefined,
        grantName: undefined,
        granttypeId: undefined,
        grantContent: undefined,
        departId: undefined,
        createUser: undefined,
        createTime: undefined,
        isEnabled: undefined,
        upStatus: "0",
        isUpphoto: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.grantId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加报名项目";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const grantId = row.grantId || this.ids
      getGrant(grantId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改报名项目";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.grantId != undefined) {
            updateGrant(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addGrant(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const grantIds = row.grantId || this.ids;
      this.$confirm('是否确认删除报名项目编号为"' + grantIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delGrant(grantIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有报名项目数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportGrant(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },// 是否启用字典翻译
    typeFormat(row, column) {
      return this.selectDictLabel(this.yesnoOptions, row.isUpphoto);
    },
    enableFormat(row,column){
      return this.selectDictLabel(this.disableOption, row.isEnabled);
    },getContent(val){
      this.form.grantContent=val;
    },
    granttypeFormat(row,column){
      var actions = [];
      Object.keys(this.grantTypeOption).map((key) => {
        if(this.grantTypeOption[key].id==row.granttypeId){
          actions.push(this.grantTypeOption[key].name);
        }
      })
      return actions.join('');
    }
  }
};
</script>
